package com.xmb.common.service.wx.impl;

import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.xmb.common.constants.RedPackStatusEnum;
import com.xmb.common.dao.XmbWechatRedPackOrderMapper;
import com.xmb.common.entity.XmbWechatRedPackOrder;
import com.xmb.common.entity.XmbXcxAccountConfig;
import com.xmb.common.entity.structure.WxPayRedPackQueryRequestSelf;
import com.xmb.common.exception.BusinessException;
import com.xmb.common.interceptor.CurrentContextUser;
import com.xmb.common.log.LoggerInterface;
import com.xmb.common.service.XcxAccountService;
import com.xmb.common.service.wx.WxCommonService;
import com.xmb.common.service.wx.WxPaySendRedpackService;
import com.xmb.common.utils.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by lyndon on 2019/4/11.
 */
@Service
public class WxPaySendRedpackServiceImpl implements WxPaySendRedpackService,LoggerInterface{

    @Resource
    private XcxAccountService accountService;

    private WxPayService wxPayService;

    @Resource
    private XmbWechatRedPackOrderMapper wechatRedPackOrderMapper;

    @Resource
    private WxCommonService wxCommonService;

    @Override
    public void sendRedPack(XmbWechatRedPackOrder sendRedpackParam)   {
        if (sendRedpackParam == null) {
            throw new BusinessException("无法获取需要发送红包的订单信息");
        }

        XmbXcxAccountConfig xcxAccountConfig = accountService.selectByAppid(sendRedpackParam.getWxappid());
        if (xcxAccountConfig == null) {
            throw new BusinessException("无法获取当前小程序的支付配置信息");
        }
        this.setWxPaySendRedpackConfig(xcxAccountConfig);
        WxPaySendRedpackRequest sendRedpackRequest = new WxPaySendRedpackRequest();
        sendRedpackRequest.setWxAppid(xcxAccountConfig.getMainAppid());
        sendRedpackRequest.setReOpenid(sendRedpackParam.getReOpenid());
        sendRedpackRequest.setSendName(sendRedpackParam.getSendName());
        sendRedpackRequest.setTotalAmount(NumberUtils.yuanToFee(sendRedpackParam.getTotalAmount().floatValue()));
        sendRedpackRequest.setTotalNum(sendRedpackParam.getTotalNum());
        sendRedpackRequest.setWishing(sendRedpackParam.getWishing());
        sendRedpackRequest.setActName(sendRedpackParam.getActName());
        sendRedpackRequest.setRemark(sendRedpackParam.getRemark());
        sendRedpackRequest.setSceneId(sendRedpackParam.getSceneId());
        sendRedpackRequest.setRiskInfo(sendRedpackParam.getRiskInfo());
        sendRedpackRequest.setAmtType(sendRedpackParam.getAmtType());
        sendRedpackRequest.setMchBillNo(wxCommonService.getRedOutTranceNo());
        sendRedpackRequest.setMsgAppid(xcxAccountConfig.getAppid());
        sendRedpackRequest.setClientIp("192.168.0.1");
        WxPaySendRedpackResult sendRedpackResult = null;
        try {
            sendRedpackResult = wxPayService.sendRedpack(sendRedpackRequest);
            if ("SUCCESS".equals(sendRedpackResult.getResultCode())) {
                // 修改发红包记录
                sendRedpackParam.setWxTransactionNo(sendRedpackResult.getSendListid());
                sendRedpackParam.setMerchantId(sendRedpackResult.getMchId());
                sendRedpackParam.setMerchantTransactionNo(sendRedpackResult.getMchBillno());
                sendRedpackParam.setRedPackStatus(RedPackStatusEnum.send.name());
                wechatRedPackOrderMapper.updateByPrimaryKeySelective(sendRedpackParam);
            }
        } catch (WxPayException e) {
            logger.info("发送失败,错误编号为:{},错误信息为:{}",e.getErrCode(),e.getErrCodeDes());
        }
    }


    @Override
    public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException {
        XmbXcxAccountConfig xcxAccountConfig = accountService.selectByAppid(CurrentContextUser.get().getAppid());
        if (xcxAccountConfig == null) {
            throw new BusinessException("无法获取当前小程序的支付配置信息");
        }
        this.setWxPaySendRedpackConfig(xcxAccountConfig);
        WxPayRedPackQueryRequestSelf redPackQueryRequestSelf = new WxPayRedPackQueryRequestSelf();
        redPackQueryRequestSelf.setMchBillNo(mchBillNo);
        return wxPayService.queryRedpack(redPackQueryRequestSelf);
    }

    /**
     * 设置企业发红包配置信息
     */
    private void setWxPaySendRedpackConfig(XmbXcxAccountConfig xcxAccountConfig ){
        wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(xcxAccountConfig.getMainAppid());
        payConfig.setMchId(xcxAccountConfig.getMainMchId());
        payConfig.setMchKey(xcxAccountConfig.getMainMchKey());
        payConfig.setKeyPath(xcxAccountConfig.getKeyPath());
        if (xcxAccountConfig.getIsSubAccount()) {
            payConfig.setSubAppId(xcxAccountConfig.getSubAppid());
            payConfig.setSubMchId(xcxAccountConfig.getSubMchId());
        }
        wxPayService.setConfig(payConfig);
    }

}
